#include "stdafx.h"
#include "History.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Path History::config;

History thehistory;

History::History()
{
	config = Path(NULL);
	config.Push("history.ini");
}

History::~History()
{

}

void History::GetKey(char key[], int N)
{
	memset(&key[0], 0, sizeof(key));

	switch(N)
	{
		case Red:
		case Black:
			strcpy(key, "redblack");
			break;
		case Big:
		case Small:
			strcpy(key, "bigsmall");
			break;
		case Even:
		case Odd:
			strcpy(key, "evenodd");
			break;
		case H1:
		case H2:
		case H3:
			strcpy(key, "hori");
			break;
		case V1:
		case V2:
		case V3:
			strcpy(key, "vert");
			break;
	}
}

int History::GetInt(const char *key, const char *subKey)
{
	return GetPrivateProfileInt(key, subKey, 0, (LPCTSTR)config);
}

void History::SetInt(const char *key, const char *subKey, int val)
{
	TCHAR buf[8];
	sprintf(buf, "%d", val);
	WritePrivateProfileString(key, subKey, buf, (LPCTSTR)config);
}

int History::GetInt(int N, const char* symbol)
{
	TCHAR key[10]={0};
	GetKey(key, N);

	return GetPrivateProfileInt(key, symbol, 0, (LPCTSTR)config);
}

void History::SetInt(int N, const char* symbol, int val)
{
	TCHAR key[10]={0};
	GetKey(key, N);

	TCHAR buf[8];
	sprintf(buf, "%d", val);

	WritePrivateProfileString(key, symbol, buf, (LPCTSTR)config);
}

void History::IncreaseInt(int N, const char* symbol)
{
	SetInt(N, symbol, GetInt(N, symbol)+1);
}

int History::GetLossTimes(int N, int D)
{
	char symbol[10]={0};
	sprintf(symbol, "%d", D);

	return GetInt(N, symbol);
}

void History::ZeroLostTimes(int N, int D)
{
	char symbol[10]={0};
	sprintf(symbol, "%d", D);

	SetInt(N, symbol, 0);
}

void History::IncreaseLossTimes(int N, int D)
{
	char symbol[10]={0};
	sprintf(symbol, "%d", D);

	IncreaseInt(N, symbol);
}

void History::DecreaseLossTimes(int N, int D)
{
	char symbol[10]={0};
	sprintf(symbol, "%d", D);

	int t = GetLossTimes(N, D)-1;
	SetInt(N, symbol, max(0,t));
}